package com.module.sysconfig.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.module.sysconfig.entity.Bdzhb;
import com.module.sysconfig.service.BdzhbService;
import com.support.helper.AjaxHelper;
import com.support.util.StringUtils;
import com.support.web.ControllerSupport;
/**
 * 系统表单组合
 */
@Controller
@Scope(value = "prototype")
@RequestMapping("/sysconfig/bdzhb")
public class BdzhbController extends ControllerSupport {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private BdzhbService bdzhbService;

	/**
	 * 保存系统表单组合表数据
	 * @throws IOException
	 */
	@RequestMapping("/saveBdzhb")
	public void saveBdzhb() throws IOException 
	{

		try 
		{
			Map<String, String> paramsMap = this.getParamsMap();
			
			Bdzhb bdzhb = new Bdzhb();
			
			String ZHMC = paramsMap.get("ZHMC");
			String JSONC = paramsMap.get("JSONC");
			String BDSL = paramsMap.get("BDSL");
			String CJR = paramsMap.get("CJR");
			String CJRMC = paramsMap.get("CJRMC");
			String ZHXGR = paramsMap.get("ZHXGR");
			String ZHXGRMC = paramsMap.get("ZHXGRMC");
			String BZ1 = paramsMap.get("BZ1");
			String BZ2 = paramsMap.get("BZ2");
			String BZ3 = paramsMap.get("BZ3");
			String BZ4 = paramsMap.get("BZ4");
			
			
			if(!StringUtils.isNumeric(BDSL))
			{
				returnJSONView(AjaxHelper.toJSON(null, AjaxHelper.FAILURE,
						"表单数量不是数字"));
				return;
			}
			
			bdzhb.setZhmc(ZHMC == null ? "":ZHMC);
			bdzhb.setJsonc(JSONC == null ? "":JSONC);
			bdzhb.setBdsl(BDSL == null ? 0:Integer.parseInt(BDSL));
			bdzhb.setCjr(CJR == null ? "":CJR);
			bdzhb.setCjrmc(CJRMC == null ? "":CJRMC);
			bdzhb.setZhxgr(ZHXGR == null ? "":ZHXGR);
			bdzhb.setZhxgrmc(ZHXGRMC == null ? "":ZHXGRMC);
			bdzhb.setSfhf(1);
			bdzhb.setBz1(BZ1 == null ? "":BZ1);
			bdzhb.setBz2(BZ2 == null ? "":BZ2);
			bdzhb.setBz3(BZ3 == null ? "":BZ3);
			bdzhb.setBz4(BZ4 == null ? "":BZ4);

			bdzhbService.saveBdzhb(bdzhb);

			returnJSONView(AjaxHelper.toJSON(bdzhb, AjaxHelper.SUCCESS,
					"表单组合数据保存！"));

		} 
		catch (Exception e) 
		{
			e.printStackTrace();
			logger.error("BdzhbController.saveBdzhb:", e);
			returnJSONView(AjaxHelper.toJSON(null, AjaxHelper.FAILURE,
					e.getMessage()));
		}
	}

	/**
	 * 更新系统表单组合表数据
	 * @throws IOException
	 */
	@RequestMapping("/updateBdzhb")
	public void updateBdzhb() throws IOException 
	{

		try 
		{
			Map<String, String> paramsMap = this.getParamsMap();
			String id = request.getParameter("ID");
			if(StringUtils.isEmpty(id))
			{
				returnJSONView(AjaxHelper.toJSON(null, AjaxHelper.FAILURE,
						"缺少主键ID"));
				return;
			}
			
			Bdzhb bdzhb = bdzhbService.queryBdzhbById(Long.parseLong(id));//根据ID查询数据
			
			String ZHMC = paramsMap.get("ZHMC");
			String JSONC = paramsMap.get("JSONC");
			String BDSL = paramsMap.get("BDSL");
			String CJR = paramsMap.get("CJR");
			String CJRMC = paramsMap.get("CJRMC");
			String ZHXGR = paramsMap.get("ZHXGR");
			String ZHXGRMC = paramsMap.get("ZHXGRMC");
			String BZ1 = paramsMap.get("BZ1");
			String BZ2 = paramsMap.get("BZ2");
			String BZ3 = paramsMap.get("BZ3");
			String BZ4 = paramsMap.get("BZ4");
			
			if(!StringUtils.isEmpty(BDSL))
			{
				if(!StringUtils.isNumeric(BDSL))
				{
					returnJSONView(AjaxHelper.toJSON(null, AjaxHelper.FAILURE,
							"表单数量不是数字"));
					return;
				}
			}
			
			bdzhb.setZhmc(ZHMC == null ? bdzhb.getZhmc():ZHMC);
			bdzhb.setJsonc(JSONC == null ? bdzhb.getJsonc():JSONC);
			bdzhb.setBdsl(BDSL == null ? bdzhb.getBdsl():Integer.parseInt(BDSL));
			bdzhb.setCjr(CJR == null ? bdzhb.getCjr():CJR);
			bdzhb.setCjrmc(CJRMC == null ? bdzhb.getCjrmc():CJRMC);
			bdzhb.setZhxgr(ZHXGR == null ? bdzhb.getZhxgr():ZHXGR);
			bdzhb.setZhxgrmc(ZHXGRMC == null ? bdzhb.getZhxgrmc():ZHXGRMC);
			bdzhb.setBz1(BZ1 == null ? bdzhb.getBz1():BZ1);
			bdzhb.setBz2(BZ2 == null ? bdzhb.getBz2():BZ2);
			bdzhb.setBz3(BZ3 == null ? bdzhb.getBz3():BZ3);
			bdzhb.setBz4(BZ4 == null ? bdzhb.getBz4():BZ4);
			

			bdzhbService.updateBdzhb(bdzhb);

			returnJSONView(AjaxHelper.toJSON(bdzhb, AjaxHelper.SUCCESS,
					"表单组合数据更新！"));

		} 
		catch (Exception e) 
		{
			e.printStackTrace();
			logger.error("BdzhbController.updateBdzhb:", e);
			returnJSONView(AjaxHelper.toJSON(null, AjaxHelper.FAILURE,
					e.getMessage()));
		}
	}

	/**
	 * 删除系统表单组合数据
	 * @throws IOException
	 */
	@RequestMapping("/deleteBdzhb")
	public void deleteBdzhb() throws IOException 
	{
		try 
		{
			Map<String, String> paramsMap = this.getParamsMap();
			String id = paramsMap.get("ID");
			if(StringUtils.isEmpty(id)){
				returnJSONView(AjaxHelper.toJSON(null, AjaxHelper.FAILURE,
						"缺少主键ID"));
				return;
			}
			
			bdzhbService.deleteBdzhb(Long.parseLong(id));
			returnJSONView(AjaxHelper.toJSON(null, AjaxHelper.SUCCESS,
					"删除表单组合数据！"));

		} 
		catch (Exception e) 
		{
			e.printStackTrace();
			logger.error("BdckController.deleteBdzhb:", e);
			returnJSONView(AjaxHelper.toJSON(null, AjaxHelper.FAILURE,
					e.getMessage()));
		}
	}

	/**
	 * 查询系统表单组合表所有数据
	 * @throws IOException
	 */
	@RequestMapping("/queryBdzhbRecord")
	public void queryBdzhbRecord() throws IOException 
	{
		try 
		{

			List<Map<String, Object>> bdzhbList = bdzhbService
					.queryBdzhbRecord();
			returnJSONView(AjaxHelper.toJSON(bdzhbList, AjaxHelper.SUCCESS,
					"表单组合数据查询！"));

		} 
		catch (Exception e) 
		{
			e.printStackTrace();
			logger.error("BdzhbController.queryBdzhbRecord:", e);
			returnJSONView(AjaxHelper.toJSON(null, AjaxHelper.FAILURE,
					e.getMessage()));
		}
	}
}
